package org.compression.services.imp;

import java.util.HashMap;
import java.util.List;

import org.compression.mapper.TxtInsertMapper;
import org.compression.services.BookService;
import org.model.AttrValueRel;
import org.model.Book;
import org.model.BookAttr;
import org.model.DescModel;
import org.model.MagazineAttrs;
import org.model.MagazineValue;
import org.model.NewAttrModel;
import org.model.TNewMagazine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional
public class BookServiceImp implements BookService{

	@Autowired
	SearchServiceImp mSearchServiceImp;

	@Autowired
	TxtInsertMapper mTxtInsertMapper;

	//老
	/*@Override
	public void updateBook(Book book) {
		//插入书名
				TNewMagazine mTNewMagazine=new TNewMagazine();
				mTNewMagazine.setName(book.getBookName());
				mSearchServiceImp.insertMaginze(mTNewMagazine);

				//查询书的id
				List<TNewMagazine>  m=(List<TNewMagazine>) mSearchServiceImp.getMaginzeName(book.getBookName());
				String id=m.get(0).getId();

				//插入书的属性
				List<String> attrs=book.getAttrs();
				int i=0;
				for(String temp:attrs) {
					MagazineAttrs model=new MagazineAttrs();
					model.setName(temp);
					model.setNew_magazine_id(id);
					model.setSort_id(i+++"");
					mSearchServiceImp.insertMagazineattrs(model);
				}
				i=0;
				//插入书的值
				List<String> values=book.getValue();
				for(String temp:values) {
					MagazineValue model=new MagazineValue();
					model.setValue(temp);
					model.setNew_magazine_id(id);
					model.setSort_id(i+++"");
					mSearchServiceImp.insertMagazinevalue(model);
				}
				i=0;
				//插入书和属性的关系

				//插入书值和书的关系

				//插入书的属性和书属性值的关系

				List<MagazineAttrs> attrsIds=mSearchServiceImp.getMaginzeAttrId(id);

				List<MagazineValue> valueIds=mSearchServiceImp.getMaginzeValue(id);

				for(MagazineAttrs attr:attrsIds) {
					for(MagazineValue value:valueIds) {
						if(attr.getSort_id().equals(value.getSort_id())) {
							AttrValueRel mAttrValueRel=new AttrValueRel();
							mAttrValueRel.setAttr_id(attr.getId());
							mAttrValueRel.setValue_id(value.getId());
							mAttrValueRel.setNew_magazine_id(id);
							mSearchServiceImp.insertAttrValueRel(mAttrValueRel);
							break;
						}
					}
				}

	}*/


	//新
	@Override
	public void insertBook(Book book) {
		//插入书名
		TNewMagazine mTNewMagazine=new TNewMagazine();
		mTNewMagazine.setName(book.getBookName());
		mSearchServiceImp.insertMaginze(mTNewMagazine);

		//查询书的id
		List<TNewMagazine>  m=(List<TNewMagazine>) mSearchServiceImp.getMaginzeName(book.getBookName());
		String id=m.get(0).getId();

		//默认属性插入值
		//批处理
		List<MagazineValue>  defulats=book.getMdeflauts();
		for(MagazineValue  defulat:defulats) {
			defulat.setNew_magazine_id(id);
			mSearchServiceImp.insertMagazinevalue(defulat);
		}

		//个性属性插入
		List<MagazineAttrs>  defulatAttrs=book.getAttrsO();
		if(defulatAttrs.size()>0) {
			for(MagazineAttrs  defulat:defulatAttrs) {
				defulat.setNew_magazine_id(id);
				mSearchServiceImp.insertMagazineattrs(defulat);
			}
			//个性属性值
			List<MagazineValue>  defulatValues=book.getValueO();
			for(MagazineValue  defulat:defulatValues) {
				defulat.setNew_magazine_id(id);
				mSearchServiceImp.insertMagazinevalue(defulat);
			}
			//关联关系插入||查询出来的
			for(int i=0;i<defulatAttrs.size();i++) {
				AttrValueRel mAttrValueRel=new AttrValueRel();
				mAttrValueRel.setAttr_id(defulatAttrs.get(i).getId());
				mAttrValueRel.setValue_id(defulatValues.get(i).getId());
				mSearchServiceImp.insertAttrValueRel(mAttrValueRel);
			}
		}

	}


	@Override
	public void insertBook(BookAttr book) {
		String name =book.getBookName();
		//	mTxtInsertMapper.insertBook(name);
		mTxtInsertMapper.insertBook(name);
		Long newMagazationId=mTxtInsertMapper.selectIdMagzine();
		List<DescModel> mDesc=book.getDefalut();
		for(DescModel value:mDesc) {
			mTxtInsertMapper.insertValue(value.getValue(), newMagazationId);
		}

		//  插入关系
		List<MagazineValue> mMagazineValues=mTxtInsertMapper.selectTNewMagazine(newMagazationId);
		for(DescModel value:mDesc) {
			for(MagazineValue mTNewMagazine:mMagazineValues) {
				if(mTNewMagazine.getValue().equals(value.getValue())) {
					mTxtInsertMapper.insertrelateIds(value.getSearchId(), mTNewMagazine.getId(), newMagazationId);
					break;
				}
			}
		}

		
		//插入结束
		List<NewAttrModel>	mNewAttrModel=book.getAdd();
		for(NewAttrModel model:mNewAttrModel) {
			mTxtInsertMapper.insertreAttravalue(model.getAttrvalue(),newMagazationId);
			mTxtInsertMapper.insertValue(model.getValue(), newMagazationId);
		}
		
		mMagazineValues=null;
		mMagazineValues=mTxtInsertMapper.selectTNewMagazine(newMagazationId);
		List<MagazineAttrs>  valuesAttrIds=mTxtInsertMapper.selectAttrIds(newMagazationId);
		for(NewAttrModel temp:mNewAttrModel) {
			int attrId=0;
			int valueId=0;
			for(MagazineValue tempvalue:mMagazineValues) {
				if(tempvalue.getValue().equals(temp.getValue())) {
					attrId=Integer.parseInt(tempvalue.getId());
				}
			}
			for(MagazineAttrs tempAttr:valuesAttrIds) {
				if(tempAttr.getName().equals(temp.getAttrvalue())) {
					valueId=Integer.parseInt(tempAttr.getId());
				}
			}
			mTxtInsertMapper.insertrelateIds(valueId, attrId+"", newMagazationId);
		}
		
		
	}






	@Override
	public void updateBook(Book book) {


	}





}
